package orm;

import org.hibernate.*;

import exception.DBPersistorException;

public class ExchangeDAO extends Exchange {

	public ExchangeDAO(String code) {
		super(code);
	}
	
	public static void persist(Exchange ex) {

		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			Transaction tx = session.beginTransaction();
			session.save(ex);
			tx.commit();
		} catch (HibernateException e) {
			throw new DBPersistorException("Could not persist exchange object.");
		} finally {
			HibernateUtil.getSessionFactory().close();
		}
		
/*			
		try {
			
			// Connection connection = DBConnectionFactory.getInstance().getConnection();

			String q1 = "SELECT id FROM currency WHERE code = \'" + ex.getCode() + "\'";
			Statement st1 = connection.createStatement();
			ResultSet res1 = st1.executeQuery(q1);
			Integer currency_id;
			if (res1.next()) {
				currency_id = res1.getInt("id");
			} else {
				String q2 = "INSERT INTO currency (code) VALUES (\'" + ex.getCode() + "\')";
				PreparedStatement prep1 = connection.prepareStatement(q2);
				prep1.executeUpdate();
				
				Statement st2 = connection.createStatement();
				ResultSet res2 = st2.executeQuery(q1);
				
				res2.next();
				currency_id = res2.getInt("id");
			}
			
			return currency_id;
		} catch(Exception e) {
			throw new DBPersistorException("Could not persist currency object");
		}
*/
	}

	public static Exchange findCreate(String code) {
		Exchange retObj = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			Transaction tx = session.beginTransaction();

			retObj = (Exchange) session.createQuery("from Exchange where code = :code")
													.setParameter("code", code)
													.uniqueResult();
			
			if (retObj != null) {
				tx.commit();
				return retObj;
			}

			retObj = new Exchange(code);
			session.save(retObj);
			// ExchangeDAO.persist(retObj);
			tx.commit();
			return retObj;
			
		} catch (HibernateException ex) {
			throw new DBPersistorException("Could not find persisted exchange object.");
		}
	}
	public static String find(Integer id) {

		Object retObj = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			Transaction tx = session.beginTransaction();
			retObj = session.load(Exchange.class, id);
			tx.commit();
		} catch (HibernateException ex) {
			throw new DBPersistorException("Could not find persisted exchange object.");
		} finally {
			HibernateUtil.getSessionFactory().close();
		}
		
		return ((Exchange) retObj).getCode();

/*
		try {

			// Connection connection = DBConnectionFactory.getInstance().getConnection();

			String query = "SELECT code FROM currency WHERE id = " + id;
			Statement st = connection.createStatement();
			ResultSet rs = st.executeQuery(query);
			if (rs.next()) {
				return rs.getString("code");
			}
			return null;
		} catch (Exception e) {
			throw new DBPersistorException("Could not retrieve currency from database.");
		}
*/
	}
	
}
